package com.joinway.framework.ueditor.servlet;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.baidu.ueditor.ActionEnter;

public class UEditorServlet extends HttpServlet {

	private final static Logger log = LoggerFactory.getLogger(UEditorServlet.class);
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String fileSavePath;
	
	private String configFilePath;
	
	private List<String> uploadPrefixes = Arrays.asList(
		new String[]{
				"imageUrlPrefix"
				, "scrawlUrlPrefix"
				, "snapscreenUrlPrefix"
				, "catcherUrlPrefix"
				, "videoUrlPrefix"
				, "fileUrlPrefix"
				, "imageManagerUrlPrefix"
				, "fileManagerUrlPrefix"
		}
	);
	
	public UEditorServlet(String configFilePath, String fileSavedPath){
		this.configFilePath = configFilePath;
		this.fileSavePath = fileSavedPath;
	}
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
//			SystemConfigRepository repository = FrameworkHelper.getBean(SystemConfigRepository.class);
			
//			String path = repository.getStringValue(SystemConfigRepository.UEditorPath);
//			String requestPath = request.getServletPath();
			
//			if(StringUtils.trimToEmpty(path).equals(requestPath)){
//				String rootPath = request.getServletContext().getRealPath("/");
				
//				String physicalPath = FileManager.getLocalPath("upload", Server.Web, request);
				
				log.info("File physical path {}", fileSavePath);
				
				FileUtils.forceMkdir(new File(fileSavePath));
				
				String output = new ActionEnter( request, configFilePath, fileSavePath).exec();
				
				if("config".equals(request.getParameter("action"))){
					// send back config.json, change the upload prefix
					String context = request.getContextPath();
					
					JSONObject json = new JSONObject(output);
					for(String prefix : uploadPrefixes){
						String uploadPath = context + json.get(prefix).toString();
						json.put(prefix, uploadPath);
					}
					
					output = json.toString();
				}
				
				response.setHeader("Content-Type" , "text/plain");
				
				IOUtils.write(output, response.getOutputStream());
//			}else{
//				response.sendError(HttpServletResponse.SC_BAD_REQUEST, "invalid path " + requestPath);
//			}
			
		}catch(Exception e){
			throw new ServletException(e);
		}
	}

	public List<String> getUploadPrefixes() {
		return uploadPrefixes;
	}

	public void setUploadPrefixes(List<String> uploadPrefixes) {
		this.uploadPrefixes = uploadPrefixes;
	}

	public String getFileSavePath() {
		return fileSavePath;
	}

	public void setFileSavePath(String fileSavePath) {
		this.fileSavePath = fileSavePath;
	}


}
